// SettingsScriptlet.cs
//

using System;
using System.DHTML;
using System.Gadgets;
using ScriptFX;
using ScriptFX.UI;

namespace MythtvGadget
{

    public class SettingsScriptlet
    {
        private InputElement url;
        private InputElement mythwebUrl;
        private SelectElement interval;
        private SelectElement guideDays;
		private InputElement gadgetHeight;
		private SelectElement dateFormat;
		private CheckBoxElement dateOnNewLine;

        static SettingsScriptlet()
        {
            if (Document.Body.ID == "gadgetSettings")
            {
                Application.Current.Run(typeof(SettingsScriptlet), null);
            }
        }

        private SettingsScriptlet()
        {
            url = (InputElement)Document.GetElementById("mythStatusUrl");
            mythwebUrl = (InputElement)Document.GetElementById("mythwebUrl");
            interval = (SelectElement)Document.GetElementById("updateInterval");
            guideDays = (SelectElement)Document.GetElementById("guideDays");
			gadgetHeight = (InputElement)Document.GetElementById("gadgetHeight");
			dateFormat = (SelectElement)Document.GetElementById("dateFormat");
			dateOnNewLine = (CheckBoxElement)Document.GetElementById("dateOnNewLine");

            url.Value = Settings.StatusUrl;
            mythwebUrl.Value = Settings.MythwebUrl;

            for (int i = 0; i < interval.Options.Length; i++)
            {
                if (((OptionElement)interval.Options[i]).Value == Settings.UpdateInterval.ToString())
                {
                    ((OptionElement)interval.Options[i]).Selected = true;
                }
            }

            for (int i = 0; i < guideDays.Options.Length; i++)
            {
                if (((OptionElement)guideDays.Options[i]).Value == Settings.GuideWarningDays.ToString())
                {
                    ((OptionElement)guideDays.Options[i]).Selected = true;
                }
			}

			gadgetHeight.Value = Settings.GadgetHeight.ToString();

			for (int i = 0; i < dateFormat.Options.Length; i++)
			{
				if (((OptionElement)dateFormat.Options[i]).Value == Settings.DateFormat)
				{
					((OptionElement)dateFormat.Options[i]).Selected = true;
				}
			}

			dateOnNewLine.Checked = Settings.DateOnNewLine;

            Gadget.OnSettingsClosing = new GadgetSettingsDialogCallback(SettingsClosing);
        }

        private void SettingsClosing(GadgetSettingsEvent e)
        {
            if (e.CloseAction == GadgetSettingsCloseAction.Commit)
            {
                Settings.StatusUrl = url.Value;
                Settings.MythwebUrl = mythwebUrl.Value;

                for (int i = 0; i < interval.Options.Length; i++)
                {
                    if (((OptionElement)interval.Options[i]).Selected)
                    {
                        Settings.UpdateInterval = (int)int.Parse(((OptionElement)interval.Options[i]).Value);
                    }
                }

                for (int i = 0; i < guideDays.Options.Length; i++)
                {
                    if (((OptionElement)guideDays.Options[i]).Selected)
                    {
                        Settings.GuideWarningDays = (int)int.Parse(((OptionElement)guideDays.Options[i]).Value);
                    }
                }

				int height = Settings.DEFAULT_HEIGHT;
				try	{ height = (int)int.Parse(gadgetHeight.Value); }
				catch (Exception) { }
				Settings.GadgetHeight = height;

                for (int i = 0; i < dateFormat.Options.Length; i++)
                {
                    if (((OptionElement)dateFormat.Options[i]).Selected)
                    {
                        Settings.DateFormat = ((OptionElement)dateFormat.Options[i]).Value;
                    }
                }

				Settings.DateOnNewLine = dateOnNewLine.Checked;
            }
        }

        public static void Main(Dictionary arguments)
        {
            SettingsScriptlet scriptlet = new SettingsScriptlet();
        }
    }
}
